$(function () {
    $("#jqGrid").jqGrid({
        url: '../hesexpert/list',     // 请求后台json数据的url
        datatype: "json",                // 后台返回的数据格式
        // 列表标题及列表模型
        colModel: [
            {label: 'id', name: 'id', index: 'ID', width: 50, key: true },
            {label: '用户名', name: 'username', index: 'username', width: 80 }, 
            {label: '密码', name: 'password', index: 'password', width: 80 }, 
            {label: '紫万帐号', name: 'zwannumber', index: 'zwannumber', width: 80 }, 
            {label: '紫万密码', name: 'zwanpassword', index: 'zwanpassword', width: 80 }, 
            {label: '邮箱', name: 'truename', index: 'truename', width: 80 }, 
            {label: '手机号', name: 'mobile', index: 'mobile', width: 80 }, 
            {label: '用户部门', name: 'dept', index: 'dept', width: 80 }, 
            {label: '职务', name: 'job', index: 'job', width: 80 }, 
            {label: '城市编码', name: 'citycode', index: 'citycode', width: 80 }, 
            {label: '擅长设备类型', name: 'adept', index: 'adept', width: 80 }, 
            {label: '创建时间', name: 'createTime', index: 'CREATE_TIME', width: 80 }, 
        ],
        viewrecords: true,     // 是否显示行号，默认值是false，不显示
        height: 385,            // 表格高度
        rowNum: 50,             // 一页显示的行记录数
        rowList : [50,100],     // 翻页控制条中 每页显示记录数可选集合
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",          // 翻页DOM
        jsonReader : {                   // 重写后台返回数据的属性
            root: "page.list",          // 将rows修改为page.list
            page: "page.currPage",      // 将page修改为page.currPage
            total: "page.totalPage",    // 将total修改为page.totalPage
            records: "page.totalCount"  // 将records修改为page.totalCount
        },
        prmNames : {              // 改写请求参数属性
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            //设置高度
            $("#jqGrid").jqGrid('setGridHeight', getWinH());
        }
    });


    $.get("../hesexpert/provinces/", function(r){
        vm.provinces = r.region;
        var data = r.provinces;
        var optionstring = "";
        for (var i in data) {
            var jsonObj =data[i];

                optionstring += "<option value=\"" + jsonObj.id + "\" >" + jsonObj.name + "</option>";

        }
        $("#province").html("<option value='请选择'>请选择...</option> "+optionstring);
    });

    $.get("/zwan/companyinfo/companyList/", function(r){
        vm.provinces = r.data;
        var data = r.data;
        var optionstring = "";
        for (var i in data) {
            var jsonObj =data[i];

            optionstring += "<option value=\"" + jsonObj.COMPANYID + "\" >" + jsonObj.COMPANYNAME + "</option>";

        }
        $("#companyId").html("<option value='请选择'>请选择...</option> "+optionstring);
    });

});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentid",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    },
    check:{
        enable:true,
        nocheckInherit:true
    }
};
var ztree;

var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        title: null,
        provinces:{},
        cities:{},
        hesExpert: {}
    },
    methods: {
        query: function () {
            //vm.reload();
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    keyword: $("#keyword").val()
                },
                page: 1
            }).trigger("reloadGrid");
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.hesExpert = {};
          //  vm.getRegionTree(null);
        },
        update: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        hesexpert : function(){

        },
        saveOrUpdate: function (event) {
            var url = vm.hesExpert.id == null ? "../hesexpert/save" : "../hesexpert/update";
            $.ajax({
                type: "POST",
                url: url,
                data: JSON.stringify(vm.hesExpert),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                     });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: "../hesexpert/delete",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function(id){
            $.get("../hesexpert/info/"+id, function(r){
                vm.hesExpert = r.hesExpert;
                //更新输入框高度
                autosize.update($('textarea'));
            });
        },
        getcities : function(provinceid){
            $.get("../hesexpert/cities/"+provinceid, function(r){
                vm.cities = r.cities;
                var data = r.cities;
                var optionstring = "";
                for (var i in data) {
                    var jsonObj =data[i];


                        optionstring += "<option value=\"" + jsonObj.citycode + "\" >" + jsonObj.name + "</option>";


                }
                $("#cities").html("<option value='请选择'>请选择...</option> "+optionstring);
            });


        },
        getRegionTree: function(roleId) {
           /* //加载菜单树
            $.get("http://localhost:8080/region/region/regionlist", function(r){
                ztree = $.fn.zTree.init($("#regionTree"), setting, r.region);
                //展开所有节点
                //ztree.expandAll(true);

               /!* if(roleId != null){
                    vm.getRole(roleId);
                }*!/
            });*/
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page
            }).trigger("reloadGrid");
        }
    }
});

// 图片自适应



// textarea 高度自动扩展
autosize($('textarea'));